<?php
$hsd = 2592000;
$this->headTitle('Tìm kiếm thiết bị','append');
?>
<script type="text/javascript">

$(document).ready(function() {
    $('input[type=checkbox]').click( function() {
        $(this).attr('value',this.checked);
        $(this).attr('check',this.checked);
    });
    $('.tabs-menu li').click(function(){
        id = $(this).attr('id');
        $(this).parent().find('li').removeClass('active');
        $(this).addClass('active');
        $(this).parent().parent().find('div.tabs').addClass('hidden');
        $(this).parent().parent().find('.tabs.'+id).removeClass('hidden');
    });
    $('select[name="categoryId[]"]').parent().find('option[value=1]').before('<option value="1-5" <?php if ($this->category && in_array("1-5",$this->category)) echo 'selected="true"';?>>Server all</option>');
});

</script>
<div class="grid grid_8" style="margin-top: 20px;">
    <div id="tabs-search">
      <ul class="tabs-menu">
        <li class="active" id="tabs-1"><span>Tìm kiếm thiết bị</span></li>
        <li id="tabs-2"><span>Server Free</span></li>
      </ul>
      <div class="tabs tabs-1">
            <form class='frm-search frm-search-equipment' name='frm-search' action='/equipment/search' method="get" enctype="application/x-www-form-urlencoded">
                <div class='search-option-wrapper'>	
                	<div style="clear: both"></div>
                	<ul class='search-option-select'>
                        <li class="first keyword">
                            <label>Từ khóa</label>
                            <input value='<?php echo $this->keyword; ?>' placeholder="<?php echo $this->keyword ? $this->keyword : 'Từ khóa ...';?>" name='keyword'/>
                        </li>
                		<li class="first">
                			<label>Nhóm</label>
                			<select class="multiple" name="categoryId[]" multiple="multiple">
                				<option value="0">Tất cả</option>
                				<?php foreach ($this->sortCate as $key=>$category){ ?>
                					<option value ="<?php echo $category['categoryId']; ?>" <?php if ($this->category && in_array($category['categoryId'],$this->category)) echo "selected='true'";?>><?php echo $category['categoryName']; ?></option>
                				<?php } ?>				
                			</select>			
                		</li>
                		<li>		
                			<label>Dự án</label>
                			<select class="multiple" name="projectId[]" multiple="multiple">
                				<option value="0" >Tất cả</option>
                				<?php foreach ($this->sortProject as $key=>$project) { ?>
                					<option value="<?php echo $project['projectId']; ?>" <?php if($this->project && in_array($project['projectId'], $this->project)) echo "selected='true'"; ?>><?php echo $project['projectName']; ?></option>
                				<?php } ?>				
                			</select>				
                		</li>
                		<li>			
                			<label>Tài sản</label>
                			<select class="multiple" name="ownerId[]" multiple="multiple">
                				<option value="" >Tất cả</option>
                				<?php foreach ($this->sortOwner  as $key=>$owner) { ?>
                					<option value="<?php echo $owner['owner']; ?>" <?php if($this->owner && in_array($owner['owner'], $this->owner)) echo "selected='true'"; ?>><?php echo $owner['owner']; ?></option>
                				<?php } ?>				
                			</select>			
                		</li>	
                		<li>
                			<label>Thiết bị dùng chung</label>
                			<select name="isGeneral">
                				<option value="-1" <?php  if($this->general == -1 ) echo "selected='true'"; ?>>--Không Chọn--</option>
                				<option value="0" <?php if($this->general == 0 ) echo "selected='true'"; ?>>Không</option>
                				<option value="1" <?php if($this->general == 1 ) echo "selected='true'"; ?> >Có</option>
                			</select>						
                		</li>
                        <li class="button">
                            <input type="submit" name="btn-search" class="submit" value="Tìm kiếm" />
                        </li>
                	   <div style="clear: both"></div>
                	</ul>
                	<div style="clear: both"></div>
                </div>
            </form>
      </div>
      <div class="tabs tabs-2 hidden">
            <form class='frm-search frm-search-equipment' name='frm-search-serverfree' action='/equipment/serverfree' method="get">
                <div class='search-option-wrapper'>	
                	<div style="clear: both"></div>
                	<ul class='search-option-select'>
                		<li class="first">
                			<label>Dự án</label>
                        </li>
            			<li>
            				<label>wingame-free</label>
            				<input class="checkbox" type="checkbox" name="project_34" value="34" />
            			</li>
            			<li>
            				<label>ssgame-free</label>
            				<input class="checkbox" type="checkbox" name="project_38" value="38" />
            			</li>
            			<li>
            				<label>ssgroup-free</label>
            				<input class="checkbox" type="checkbox" name="project_47" value="47" />
            			</li>
                        <li class="button">
                            <input type="submit" name="btn-search" class="submit" value="Tìm kiếm" />
                        </li>
                	   <div style="clear: both"></div>
                	</ul>
                	<div style="clear: both"></div>
                </div>
            </form>
      </div>
    </div>
</div>
<div class="grid grid_8" style="margin-top: 20px;">
    <?php if($this->equipment_list && $this->equipment_list){ ?>
    <div class="box table">
        <table cellspacing="0" class="content_equipment">
        	<thead id="detail">
        		<tr>
        			<td>STT</td>
        			<?php
            			
                        //equipment
                        if(in_array('code',$this->arr_name))
                            echo  '<td>Mã quản lý</td>';
                        if(in_array('equipmentName',$this->arr_name))
                            echo  '<td>Tên thiết bị</td>';
                        if(in_array('position',$this->arr_name))
                            echo  '<td>Vị trí</td>';
                        if(in_array('function',$this->arr_name))
                            echo  '<td>Chức năng</td>';
                        if(in_array('endUser',$this->arr_name))
                            echo  '<td>Người sử dụng</td>';
                        if(in_array('username',$this->arr_name))
                            echo  '<td>Quản lý kỹ thuật</td>';
                        if(in_array('note',$this->arr_name))
                            echo  '<td>Chú thích</td>';
                        if(in_array('params',$this->arr_name))
                            echo  '<td>Công ty dùng chung</td>';
                        if(in_array('categoryName',$this->arr_name))
                            echo  '<td>Nhóm</td>';
                        if(in_array('projectName',$this->arr_name))
                            echo  '<td>Dự án</td>';
                        if(in_array('ip',$this->arr_name))
                            echo  '<td>IP Lan</td>';
                        if(in_array('ipWan',$this->arr_name))
                            echo  '<td>IP Wan</td>';

                        //item
                        if(in_array('itemName',$this->arr_name))
                            echo  '<td>Item</td>';
                        if(in_array('info',$this->arr_name))
                            echo  '<td>Thông tin</td>';
                        if(in_array('rentItem',$this->arr_name))
                            echo  '<td>Giá thuê/tháng</td>';
                        if(in_array('priceItem',$this->arr_name))
                            echo  '<td>Giá mua</td>';
                        if(in_array('priceNow',$this->arr_name))
                            echo  '<td>Giá hiện tại/tháng</td>';
                        if(in_array('rentEnd',$this->arr_name))
                            echo  '<td>Giá sau hợp đồng</td>';
                        if(in_array('holders',$this->arr_name))
                            echo  '<td>Người ký hợp đồng</td>';
                        if(in_array('startDate',$this->arr_name))
                            echo  '<td>Ngày bắt đầu</td>';
                        if(in_array('duration',$this->arr_name))
                            echo  '<td>Ngày kết thúc</td>';
                        if(in_array('history',$this->arr_name))
                            echo  '<td>Lịch sử</td>';
                        if(in_array('owner',$this->arr_name))
                            echo  '<td>Tài sản</td>';
                        if(in_array('borrow',$this->arr_name))
                            echo  '<td>Công ty đang sử dụng</td>';
                        if(in_array('addendums',$this->arr_name))
                            echo  '<td>Phụ lục</td>';
                        if(in_array('serialNumber',$this->arr_name))
                            echo  '<td>serial Number</td>';
        			?>
        			<td>Tuỳ chọn</td>
        		</tr>
        	</thead>
        	<tbody>
            
            <?php 
                $i=0; 
                $temp = 'odd';
                foreach($this->equipment_list as $equipment){ 
            ?>
                <tr id="row<?php echo $equipment['equipmentId']; ?>" class="row-<?php echo $temp;?>"<?php if($equipment['trackDate'] > 0 && $equipment['trackDate'] <= date("Y-m-d")) echo "style='background:#cef6ff'";elseif($equipment['isGeneral'] == 1) echo "style='background:#FFF8C6'"; ?>>
        			<td><?php echo ++$i;?></td>
                    
                    <?php
                        //equipment
                        if(isset($equipment['code']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['code']),'">',
                                    $equipment['code'] ? $this->stringTrim(str_replace("\r\n","",$equipment['code']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['equipmentName']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['equipmentName']),'">',
                                    $equipment['equipmentName'] ? $this->stringTrim(str_replace("\r\n","",$equipment['equipmentName']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['position']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['position']),'">',
                                    $equipment['position'] ? $this->stringTrim(str_replace("\r\n","",$equipment['position']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['function']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['function']),'">',
                                    $equipment['function'] ? $this->stringTrim(str_replace("\r\n","",$equipment['function']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['endUser']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['endUser']),'">',
                                    $equipment['endUser'] ? $this->stringTrim(str_replace("\r\n","",$equipment['endUser']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('username',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['username']),'">',
                                    $equipment['username'] ? $this->stringTrim(str_replace("\r\n","",$equipment['username']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['note']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['note']),'">',
                                    $equipment['note'] ? $this->stringTrim(str_replace("\r\n","",$equipment['note']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('params',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['params']),'">',
                                    $equipment['params'] ? $this->stringTrim(str_replace("\r\n","",$equipment['params']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('categoryName',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['categoryName']),'">',
                                    $equipment['categoryName'] ? $this->stringTrim(str_replace("\r\n","",$equipment['categoryName']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('projectName',$this->arr_name)){
                            echo '<td>';
                            $stt = 1;
                            foreach($equipment['project_list'] as $project){
                                if($stt == 1)
                                    echo '"',$this->stringTrim(str_replace("\r\n","",$project["projectName"]), 40),'"';
                                else
                                    echo ' - "',$this->stringTrim(str_replace("\r\n","",$project["projectName"]), 40),'"';
                                ++$stt;
                            }
                            echo '</td>';
                        }
                        if(isset($equipment['ip']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['ip']),'">',
                                    $equipment['ip'] ? $this->stringTrim(str_replace("\r\n","",$equipment['ip']), 40) : '-','
                                    </span>
                                </td>';
                        if(isset($equipment['ipWan']))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['ipWan']),'">',
                                    $equipment['ipWan'] ? $this->stringTrim(str_replace("\r\n","",$equipment['ipWan']), 40) : '-','
                                    </span>
                                </td>';
                                
                                
                        //item
                        if(in_array('itemName',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							$total = $hsd + 86400;
    							if($item['duration'] > 0){
    								$currDate = strtotime(date("Y-m-d"));
    								$duration = strtotime($item['duration']);
    								$total = ($duration - $currDate);
    							}
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['itemName']),'">';
                                                if($item['expire'] == 1 || (int)($item['duration']) == 0 ){
                        							echo $item['itemName'] ? $this->stringTrim(str_replace("\r\n","",$item['itemName']), 9) : '-';
                								}else{
                									if(($total <= $hsd && $total >=0) && $item['expire'] == 0){
                										$expire['flag'] = 1;
                										$expire['equipmentId'] = $equipment['equipmentId'];										
                							            echo'<font color=red style="position:relative">', $item['itemName'] ? $item['itemName'] . "\n" . '<a class="ngay" title="Còn '.($total - 86400)/86400 . ' ngày nữa là chuyển sang giai đoạn thuê chỗ"><b>'.($total - 86400)/86400 . '</b></a>' : '-','</font>';
                							        }else if(($total <= $hsd && $total < 0) && $item['expire'] == 0){
                										$expire['flag'] = 1;
                										$expire['equipmentId'] = $equipment['equipmentId'];
                                                        echo '<font color=red style="position:relative">',$item['itemName'] ? $item['itemName'] . "\n" . '<a class="ngay" title="Đã chuyển qua giai đoạn thuê chỗ được '. ($total * (-1) - 86400)/86400 . ' ngày"><b> -'. ($total * (-1) - 86400)/86400 . '</b></a>' : '-','</font>';
                							        }else{
                										echo $item['itemName'] ? $item['itemName'] : '-';
                									}
                								}
                                echo       '</span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('info',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['info']),'">',
                                            $item['info'] ? $this->stringTrim(str_replace("\r\n","",$item['info']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('rentItem',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['rentItem'] ? number_format($item['rentItem'], 0, ',', '.') : '-','">',
                                            $item['rentItem'] ? number_format($item['rentItem'], 0, ',', '.') : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('priceItem',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['priceItem'] ? number_format($item['priceItem'], 0, ',', '.') : '-','">',
                                            $item['priceItem'] ? number_format($item['priceItem'], 0, ',', '.') : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('priceNow',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['priceNow'] ? number_format($item['priceNow'], 0, ',', '.') : '-','">',
                                            $item['priceNow'] ? number_format($item['priceNow'], 0, ',', '.') : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('rentEnd',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['rentEnd'] ? number_format($item['rentEnd'], 0, ',', '.') : '-','">',
                                            $item['rentEnd'] ? number_format($item['rentEnd'], 0, ',', '.') : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('holders',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['holders']),'">',
                                            $item['holders'] ? $this->stringTrim(str_replace("\r\n","",$item['holders']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('startDate',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['startDate'] ? $this->time()->dataToDate($item['startDate']) : '-','">',
                                            $item['startDate'] ? $this->time()->dataToDate($item['startDate']) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('duration',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',$item['duration'] ? $this->time()->dataToDate($item['duration']) : '-','">',
                                            $item['duration'] ? $this->time()->dataToDate($item['duration']) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('history',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['history']),'">',
                                            $item['history'] ? $this->stringTrim(str_replace("\r\n","",$item['history']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('owner',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['owner']),'">',
                                            $item['owner'] ? $this->stringTrim(str_replace("\r\n","",$item['owner']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('borrow',$this->arr_name))
                            echo '<td>
                                    <span title="',str_replace('"',"'",$equipment['borrow']),'">',
                                    $equipment['borrow'] ? $this->stringTrim(str_replace("\r\n","",$equipment['borrow']), 40) : '-','
                                    </span>
                                </td>';
                        if(in_array('addendums',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['addendums']),'">',
                                            $item['addendums'] ? $this->stringTrim(str_replace("\r\n","",$item['addendums']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                        if(in_array('serialNumber',$this->arr_name)){
                            echo '<td><table>';
                            foreach($equipment['items_list'] as $item){
    							echo '<tr>
    							       <td style="border-left: 0px;">
                                         <div style="height: 30px;">
                                           <span title="',str_replace('"',"'",$item['serialNumber']),'">',
                                            $item['serialNumber'] ? $this->stringTrim(str_replace("\r\n","",$item['serialNumber']), 9) : '-','
                                           </span>
                                         </div>
                                       </td>
    						        </tr>';
                            }
                            echo '</table></td>';
                        }
                    ?>
                    
                    
    			<td class="tc">
    				<a href="<?php echo $this->url(array ( 'controller' => 'equipment' ,'action' => 'detail' , 'id' => $equipment['equipmentId'] )); ?>">
                        <img src="image/icons/small/icon_info.png" title="xem chi tiết" border="0">
                    </a>
    				<a href="<?php echo $this->url(array ( 'controller' => 'equipment' ,'action' => 'edit' ,'id' => $equipment['equipmentId']));?>">
                        <img src="image/icons/small/icon_edit.png" title="sửa" border="0">
                    </a>
    				<a href="<?php echo $this->url(array ( 'controller' => 'equipment' ,'action' => 'delete' , 'id' => $equipment['equipmentId'] )); ?>">
                        <img src="image/icons/small/icon_delete.png" title="xóa" border="0">
                    </a>
    			</td>
            </tr>
            <?php 
                    if($temp == 'odd')
                        $temp = 'even';
                    else
                        $temp = 'odd';
                } 
            ?>
            
             </tbody>
             <!-- BEGIN: PAGINATION (FOOT) -->
             <tfoot>
        		<tr>
        			<td colspan="<?php echo count($this->arr_header)+2?>">
        				<div class="container">
        					<div class="pagination">
        						<?php
        							echo $this->paginationControl($this->panigator,'Sliding','paginator.phtml', array('sel' => 'search', 'keyword' => $this->keyword, 'projectId' => $this->project, 'categoryId' => $this->category, 'ownerId' => $this->owner, 'isGeneral' => $this->general));
        						?>
        					</div>
        				</div>
        			</td>
        		</tr>
        	  </tfoot>
        	  <!-- END: PAGINATION (FOOT) -->
        </table>
    	<p align="center" style="clear:none;">
            <a style="text-decoration: none; color: #1f92ff;" href='<?php echo $this->url(array('action'=>'exportexcel','type'=>'searchequipment','search_keyword'=>$this->keyword,'search_project'=>$this->project,'search_category'=>$this->category,'search_owner'=>$this->owner,'search_general'=>$this->general))?>' title="xuất Excel">
                <img src='image/icons/small/icon_table-export.png' /><br />Xuất file excel
            </a>
        </p>
    </div>
    <?php }elseif($this->search == 0){ ?>
    
    <?php }else{ ?>
    <div class="notification error"><span class="strong">Chưa có dữ liệu</span></div>
    <?php } ?>
</div>
<div class="grid">
    <div class="box-header">
        Hướng dẫn
    </div>
    <div class="box ">
        - Thiết bị có <span class="red">màu đỏ</span> là những item sắp (đã) chuyển qua giai đoạn thuê chỗ.<br />
        <div class="bg-yellow">- Những dòng có nền màu vàng nhạt là những thiết bị dùng chung.</div>
        <div class="bg-green">- Những dòng có nền màu xanh nhạt là những thiết bị được theo dõi (sẽ được cập nhật trong thời gian tới).</div>
    </div>
</div>
<script>
	var w = $(".content_equipment").width();
	$(".grid .box.table").css('width', w);
</script>